import { asyncRoutes, constantRoutes } from 'src/router'
import Layout from 'src/layout/index.vue'

function hasPermission(ids, route) {
    return ids.includes(route.id)
}

const allViews = import.meta.glob('../../views/**/*.vue')

export function filterAsyncRoutes(parent, routes) {

    /*const res = []
    routes.forEach(route => {
        const tmp = { ...route }
        if (hasPermission(roles, tmp)) {
            if (tmp.children) {
                tmp.children = filterAsyncRoutes(tmp.children, roles)
            }
            res.push(tmp)
        }
    })*/
    const res = []
    routes.forEach(route => {
        const tmp = { ...route }
        const item = {};
        if(tmp.path != ''){
            item.parent = parent
            if(tmp.children && tmp.children.length > 0){
                item.id = tmp.id;
                item.path = tmp.path;
                if(tmp.menuType == 'M'){
                    item.component = Layout;
                    item.alwaysShow = true;
                } else {
                    item.component = allViews['../../views' + tmp.component + '.vue']
                }
                if(tmp.menuType == 'M'){
                    item.redirect = tmp.children[0].path;
                }
                item.name = tmp.name;
                item.meta = {'title':tmp.menuName,'icon':tmp.icon};
                item.hidden = tmp.hidden == 1
                let children = filterAsyncRoutes(item, tmp.children)
                if(item.children && children.length >0){
                    item.children = children.concat(item.children)
                }else if(!item.children){
                    item.children = children
                }
            }else{
                item.id = tmp.id;
                item.path = tmp.path;
                item.component = allViews['../../views' + tmp.component + '.vue']
                item.name = tmp.name;
                item.meta = {'title':tmp.menuName,'icon':tmp.icon};
                item.hidden = (tmp.menuType == 'F' || tmp.hidden == 1)
                if(parent){
                    item.meta.activeMenu = parent.path
                }
            }
            if(tmp.menuType == 'C'){
                delete item.meta.activeMenu
            }else if(parent){
                if(parent.menuType != 'M'){
                    item.meta.activeMenu = parent.path
                }
                if(parent.hidden == true){
                    item.meta.activeMenu = '/dashboard';
                }
            }

            let p = item.parent;
            while (p){
                if(!p.parent){
                    break
                }
                p = p.parent
            }
            if(p && p != parent){
                if(!p.children){
                    p.children = []
                }
                p.children.push(item)
            }else{
                res.push(item)
            }
        }
    })
    return res
}

const state = {
    routes: [],
    addRoutes: []
}

const mutations = {
    SET_ROUTES: (state, routes) => {
        state.addRoutes = routes
        state.routes = constantRoutes.concat(routes)
    }
}

const actions = {
    generateRoutes({ commit }, menus) {
        return new Promise(resolve => {
            let accessedRoutes = filterAsyncRoutes(null, menus)
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })
    }
    /*generateRoutes({ commit }, ids) {
        return new Promise(resolve => {
            let accessedRoutes = filterAsyncRoutes(asyncRoutes, ids)
            commit('SET_ROUTES', accessedRoutes)
            resolve(accessedRoutes)
        })
    }*/
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
